// etcdtozkevent
//etcd的pod值注册到zk
package main

import (
	"strings"
)

type EtcdAndZkMaster struct {
	etcdMaster *EtcdMaster
	zkMaster   *ZkMaster
}

func NewEtcdAndZkMaster() *EtcdAndZkMaster {
	return &EtcdAndZkMaster{}
}

func (ez *EtcdAndZkMaster) SetEtcdMaster(e *EtcdMaster) {
	ez.etcdMaster = e
}

func (ez *EtcdAndZkMaster) SetZkMaster(z *ZkMaster) {
	ez.zkMaster = z
}

//etcd里的POD信息注册到zk
func (ez *EtcdAndZkMaster) AddEtcdReisterZk(podMember *Member) bool {
	ports := strings.Split(podMember.Port, "-")
	for _, port := range ports {

		podPath := mainStatic.ZK_WATCH_PATH + "/" + podMember.AppName

		ok, _, _ := ez.zkMaster.zkConnect.Exists(podPath)

		if !ok {
			//非session
			ez.zkMaster.CreateZkPath(podPath)
		}
		podPath += "/" + podMember.IP + ":" + port

		ok, _, _ = ez.zkMaster.zkConnect.Exists(podPath)

		if ok == true {
			ez.zkMaster.DeleteZkSessionPath(podPath)
		}
		ez.zkMaster.CreateZkSessionPath(podPath)
	}
	return true
}

//删除etcd对zk信息的注册
func (ez *EtcdAndZkMaster) DeleteEtcdReisterZk(podMember *Member) bool {
	ports := strings.Split(podMember.Port, "-")
	for _, port := range ports {
		podPath := mainStatic.ZK_WATCH_PATH + "/" + podMember.AppName + "/" + podMember.IP + ":" + port
		ez.zkMaster.DeleteZkSessionPath(podPath)
	}
	return true
}

//关闭引用
func (ez *EtcdAndZkMaster) Close() {
	ez.etcdMaster = nil
	ez.zkMaster = nil

}
